package org.framework.lazy.cloud.network.heartbeat.server.netty.proxy.http.advanced;


import io.netty.channel.Channel;
import lombok.extern.slf4j.Slf4j;
import org.framework.lazy.cloud.network.heartbeat.common.ChannelContext;
import org.framework.lazy.cloud.network.heartbeat.common.NettyByteBuf;
import org.framework.lazy.cloud.network.heartbeat.common.NettyTransferChannelContext;
import org.framework.lazy.cloud.network.heartbeat.common.adapter.ChannelProxyFlowAdapter;
import org.framework.lazy.cloud.network.heartbeat.common.advanced.payload.NettyChannelContext;
import org.framework.lazy.cloud.network.heartbeat.common.advanced.payload.NettyProxyMsg;
import org.framework.lazy.cloud.network.heartbeat.common.advanced.proxy.http.server.AbstractHandleHttpReportClientProxyClientConnectionTransferTypeAdvanced;
import org.framework.lazy.cloud.network.heartbeat.common.constant.ProxyMessageType;
import org.framework.lazy.cloud.network.heartbeat.common.enums.ChannelFlowEnum;
import org.framework.lazy.cloud.network.heartbeat.common.enums.ChannelProtocolType;
import org.framework.lazy.cloud.network.heartbeat.common.enums.ChannelProxyType;
import org.framework.lazy.cloud.network.heartbeat.common.utils.ChannelAttributeKeyUtils;
import org.framework.lazy.cloud.network.heartbeat.server.netty.flow.proxy.ServerChannelProxyFlow;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.context.annotation.Role;
import org.springframework.stereotype.Component;


/**
 * 服务端处理客户端代理到服务端的请求
 * HTTP_REPORT_CLIENT_PROXY_CLIENT_TRANSFER_CONNECTION_
 */
@Role(BeanDefinition.ROLE_INFRASTRUCTURE)
@Slf4j
@Component
public class ServerHandleHttpReportClientProxyClientTransferRequestTypeAdvanced
        extends AbstractHandleHttpReportClientProxyClientConnectionTransferTypeAdvanced<NettyProxyMsg> {

    private final ChannelProxyFlowAdapter channelProxyFlowAdapter;

    public ServerHandleHttpReportClientProxyClientTransferRequestTypeAdvanced(ChannelProxyFlowAdapter channelProxyFlowAdapter) {
        this.channelProxyFlowAdapter = channelProxyFlowAdapter;
    }


    /**
     * 处理当前数据
     *
     * @param nettyChannelContext 当前通道
     * @param nettyProxyMsg       通道数据
     */
    @Override
    public void doHandler(NettyChannelContext nettyChannelContext, NettyProxyMsg nettyProxyMsg) {
        Channel channel = nettyChannelContext.channel();
        // 创建链接、发送数据
        String targetPortString = nettyProxyMsg.targetPort();
        Integer targetPort = Integer.parseInt(targetPortString);
        String targetIpString = nettyProxyMsg.targetIp();
        byte[] data = nettyProxyMsg.getData();
        byte[] visitorId = nettyProxyMsg.getVisitorId();

        String clientId = nettyProxyMsg.clientId();
        String msgVisitorId = new String(visitorId);
        ChannelAttributeKeyUtils.buildClientId(channel, clientId);
        ChannelAttributeKeyUtils.buildVisitorId(channel, msgVisitorId);
        ChannelAttributeKeyUtils.buildTargetClientId(channel, clientId);
        ChannelAttributeKeyUtils.buildTargetIp(channel, targetIpString);
        ChannelAttributeKeyUtils.buildTargetPort(channel, targetPort);

        NettyTransferChannelContext.pushVisitor(channel, msgVisitorId);
        NettyByteBuf nettyByteBuf = new NettyByteBuf();
        nettyByteBuf.setData(data);
        ChannelAttributeKeyUtils.buildNettyByteBufData(channel, nettyByteBuf);
        // 获取客户端心跳通道
        Channel loadBalance = ChannelContext.getLoadBalance(clientId);

        NettyProxyMsg clientConnectTagetNettyProxyMsg = new NettyProxyMsg();
        clientConnectTagetNettyProxyMsg.setVisitorId(msgVisitorId);
        clientConnectTagetNettyProxyMsg.setTargetIp(targetIpString);
        clientConnectTagetNettyProxyMsg.setTargetPort(targetPort);
        clientConnectTagetNettyProxyMsg.setClientId(clientId);
        clientConnectTagetNettyProxyMsg.setType(ProxyMessageType.HTTP_DISTRIBUTE_CLIENT_PROXY_CLIENT_TRANSFER_CONNECTION_SUCCESSFUL_);
        if (loadBalance != null) {
            // 让客户端主动创建一个数据传输通道
            loadBalance.writeAndFlush(clientConnectTagetNettyProxyMsg);
        } else {
            log.error("can not find target client:【】 channel", clientId);
        }
        // 记录进口数据
        ServerChannelProxyFlow serverChannelFlow = ServerChannelProxyFlow
                .builder()
                .channelFlowEnum(ChannelFlowEnum.IN_FLOW)
                .port(targetPort)
                .ip(targetIpString)
                .clientId(clientId)
                .channelProxyType(ChannelProxyType.CLIENT_PROXY_CLIENT)
                .channelProtocolType(ChannelProtocolType.HTTP)
                .flow(data.length)
                .build();
        channelProxyFlowAdapter.asyncHandler(channel, serverChannelFlow);

    }

}
